package com.huadao.pda.material;

import com.huadao.pda.data.bean.Dispensing;
import com.huadao.pda.data.http.RockBaseObserver;
import com.huadao.pda.modle.DispensingModle;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import io.reactivex.Observable;

public class MaterialPresenter extends IMaterialContract.IMaterialPresenter {

    private DispensingModle dispensingModle;

    public MaterialPresenter(IMaterialContract.IMaterialView baseView) {
        attachView(baseView);
        dispensingModle=new DispensingModle(baseView.getContext());
    }

    @Override
    void getMaterialList() {
        Observable<List<Dispensing>> observable= dispensingModle.getDispensing(baseView.getProjectId());
        dispensingModle.toSubscribe(observable, new RockBaseObserver<List<Dispensing>>(this) {
            @Override
            protected void onBaseNext(List<Dispensing> data) {
                baseView.onGetMaterialList(data);
            }
        });
    }

    @Override
    public void getMaterialListByDay(final int day) {
        Observable<List<Dispensing>> observable= dispensingModle.getDispensing(baseView.getProjectId());
        dispensingModle.toSubscribe(observable, new RockBaseObserver<List<Dispensing>>(this) {
            @Override
            protected void onBaseNext(List<Dispensing> data) {
                Calendar projectStartTime=Calendar.getInstance();
                projectStartTime.setTime(baseView.getProjectStartTime());

                Date startDate;
                Date endDate;
                if(day==1){
                    startDate=projectStartTime.getTime();
                    projectStartTime.set(Calendar.HOUR_OF_DAY,0);
                    projectStartTime.set(Calendar.MINUTE,0);
                    projectStartTime.set(Calendar.SECOND,0);
                    projectStartTime.add(Calendar.DAY_OF_MONTH,1);
                    endDate=projectStartTime.getTime();
                }else {
                    projectStartTime.set(Calendar.HOUR_OF_DAY,0);
                    projectStartTime.set(Calendar.MINUTE,0);
                    projectStartTime.set(Calendar.SECOND,0);
                    projectStartTime.add(Calendar.DAY_OF_MONTH,day-1);
                    startDate=projectStartTime.getTime();
                    projectStartTime.add(Calendar.DAY_OF_MONTH,1);
                    endDate=projectStartTime.getTime();
                }
                List<Dispensing> dispensingList=getDispensingBetween(data,startDate,endDate);
                baseView.onGetMaterialList(dispensingList);
            }
        });
    }

    private List<Dispensing> getDispensingBetween(List<Dispensing> data,Date startDate,Date endDate){
        List<Dispensing> dispensingList=new ArrayList<>();
        for(Dispensing dispensing:data){
            if(dispensing.getUseTime()!=null&&dispensing.getUseTime().after(startDate)&&dispensing.getUseTime().before(endDate)){
                dispensingList.add(dispensing);
            }
        }
        return dispensingList;
    }

    @Override
    void consumeMaterial(final Dispensing dispensing) {
        Observable<Boolean> observable=dispensingModle.consumingDispensing(dispensing.getBarCode());
        dispensingModle.toSubscribe(observable, new RockBaseObserver<Boolean>(this) {
            @Override
            protected void onBaseNext(Boolean data) {
                baseView.onConsumeMaterial(dispensing,data);
            }
        });
    }

    @Override
    public void finishGet() {
        baseView.onFinishGet();
    }
}
